//app.js
App({
  globalData: {
    userInfo: {
      avatarUrl:null,
      nickName: null,
      openId: null,
      unionId: null,
    }
  },
  onShareAppMessage: function (res) {
    return {
      title: '赏金达人',
      path: '/pages/form/form_table/form_table',
      imageUrl: 'https://zs.cdbbox.com/uploads/images/20200323194452.jpg',
    }
  },
  onLaunch: function () {
    var that = this
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    
    wx.login({
      
      success: res => {
        if (res.code) {
          
            wx.getWeRunData({
              success(resRun) {
                  wx.getUserInfo({ //得到rawData, signatrue, encryptData
                    success: function (data) {
                      
                      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                      // 所以此处加入 callback 以防止这种情况
                      // if (this.userInfoReadyCallback) {
                      //   this.userInfoReadyCallback(data)
                      // }
                      var rawData = data.rawData;
                      var signature = data.signature;
                      var encryptedData = data.encryptedData;
                      var iv = data.iv;
                      var rencryptedData = resRun.encryptedData;
                      var riv = resRun.iv;
                     
                      wx.request({
                        url: 'https://zs.cdbbox.com/index/api/vlogin',
                        data: {
                          "code": res.code,
                          "rawData": rawData,
                          "signature": signature,
                          'iv': iv,
                          'encryptedData': encryptedData,
                          'rencryptedData': rencryptedData,
                          'riv': riv,
                        },
                        method: 'POST',
                        success: function (info) {
                          var userInfo = info.data
                          that.globalData.userInfo = userInfo
                          wx.setStorageSync('avatarUrl', userInfo.avatarUrl);
                          wx.setStorageSync('nickName', userInfo.nickName);
                          wx.setStorageSync('openId', userInfo.openId);
                          wx.setStorageSync('unionId', userInfo.unionId);
                          //console.log(that.globalData.userInfo);
                        }
                      })
                    },fail:res1=>{
                      //console.log(res1);
                    }
                  })
              },fail:res2=>{
                wx.showModal({
                  title: '提示',
                  content: '亲！您未开通微信运动，请关注“微信运动”公众号后重试',
                  showCancel: false,
                  confirmText: '知道了'
                })
                
              }
            })
        }
      },
      fail:res3=>{
        //console.log(res3);
      }
    })


    //获取用户信息
    // wx.getSetting({
    //   success: res => {
    //     if (res.authSetting['scope.userInfo']) {
    //       // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
    //       wx.getUserInfo({
    //         success: res => {
    //           // 可以将 res 发送给后台解码出 unionId
              
    //         }
    //       })
    //     }
    //   }
    // })
  },
  getUserInfo: function (e) {
    //console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})